package com.netnovelstore.controller;

import com.netnovelstore.annotation.ResultInterceptor;
import com.netnovelstore.pojo.Book;
import com.netnovelstore.pojo.Exception.FailureBehaviorException;
import com.netnovelstore.pojo.Result;
import com.netnovelstore.service.BookService;
import com.netnovelstore.viewObject.UploadBookFileResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

@RestController
@ResultInterceptor
@RequestMapping("/Manage/Book")
@CrossOrigin
@Slf4j
@Api(tags = "书籍管理接口")
public class ManageBookController {
    private BookService bookServiceImpl;

    @Autowired
    public ManageBookController(BookService bookServiceImpl) {
        this.bookServiceImpl = bookServiceImpl;
    }

    @PutMapping("/addBook")
    @ApiOperation("添加书籍")
    public Result<Boolean> addBook(@RequestBody Book book) throws Exception {
        log.info(book.toString());
        return bookServiceImpl.addBook(book);
    }

    @PostMapping("/uploadBookFile")
    @ApiOperation("书籍上传")
    public Result<UploadBookFileResult> uploadBookFile(@RequestPart @RequestParam("file") MultipartFile file, HttpServletResponse response) throws IOException {
        log.info(file.getOriginalFilename());
        return bookServiceImpl.uploadBookFile(file);
    }

    @PatchMapping("/setBookByNumber")
    @ApiOperation("按 书籍编号 编辑书籍")
    public Result<Boolean> setBookByNumber(@RequestBody Book book) {
        log.info(book.toString());
        return bookServiceImpl.setBookByNumber(book);
    }

    @DeleteMapping("/delBooksByNumberList")
    @ApiOperation("删除书籍")
    public Result<Boolean> delBooksByNumberList(@RequestBody @NotEmpty List<Integer> numberList) throws FailureBehaviorException {
        return bookServiceImpl.delBooksByNumberList(numberList);
    }

    @GetMapping("/setQuantityByNumber")
    @ApiOperation("按 书籍编号 修改访问人数")
    public Result<Boolean> setQuantityByNumber(@NotNull Integer quantity, @NotNull Integer bookNumber) throws FailureBehaviorException {
        return bookServiceImpl.setQuantityByNumber(quantity, bookNumber);
    }

    @PostMapping("/uploadBookCover")
    @ApiOperation("书籍封面上传")
    public Result<String> uploadBookCover(@RequestPart @RequestParam MultipartFile file, HttpServletResponse response) throws IOException {
        return bookServiceImpl.uploadBookCover(file);
    }

}
